var xhr;

function validateLogin() {
    
    //create xhr
    if (window.ActiveXObject) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest){
        xhr = new XMLHttpRequest;
    }

    //build query string
    var strEmail = document.getElementById("txtEmail").value;
    var strPassword = document.getElementById("txtPassword").value;
    var strRememberMe = document.getElementById("chkRememberMe").checked;
    var strQuery = "txtEmail=" + strEmail + "&txtPassword=" + strPassword + "&chkRememberMe=" + strRememberMe;
    
    //setup xhr
    
    xhr.open("POST", "ServletCustomerLogin?timeStamp=" + new Date().getTime(), true);
    xhr.onreadystatechange = handleLoginResponse;
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    xhr.send(strQuery);

    //processing...
    document.getElementById("divError").style.display = "block";
    document.getElementById("divError").innerHTML = "Processing...";
}
/*****************************************************************************/
function handleLoginResponse(){

    if (xhr.readyState != 4) return;
    if (xhr.status != 200) return;
    
    //handle the response
    var xhresponse = xhr.responseXML;
    var nodError = xhresponse.getElementsByTagName("Error1");
    var divError = document.getElementById("divError");
    
    if (nodError.length > 0) {
        divError.innerHTML = nodError[0].firstChild.nodeValue;
        divError.style.display = "block";
        document.getElementById("txtPassword").value = "";
    } else {
        divError.innerHTML = "";
        divError.style.display = "none";

        /* redirects and display welcome message for 3 sec */
        setTimeout("window.location = 'index.html';", 2000);
        displayPopup("Welcome! " + getCookie("FirstName") + ", you are now logged in", "right", 2000);
    }  
}
/*****************************************************************************/
function checkLogin() {

    var strFirstName = ""; 
    var aLogin = document.getElementById("aLogin");
    var aLogout = document.getElementById("aLogout");
    var divWelcome = document.getElementById("divWelcome");
    var lstManager = document.getElementById("lstManager");

    //hide manager task
    document.getElementById("lstManager").style.display = "none";

    strFirstName = getCookie("FirstName");

    if (strFirstName != "") {
        aLogout.style.display = "inline";
        aLogin.style.display = "none";
        divWelcome.innerHTML = "Hello! " + strFirstName
        divWelcome.style.display = "inline";
    
        //display manager task is UserLevel > 1
        if (parseInt(getCookie("UserLevel")) > 1)
            document.getElementById("lstManager").style.display = "inline";
    }
    else {
        aLogin.style.display = "inline";
        aLogout.style.display = "none";
        divWelcome.style.display = "none";
    }        
}
/*****************************************************************************/
function logout(strRedirect) {
   
    var strFirstName = getCookie("FirstName");
    var strUserLevel = getCookie("UserLevel");

    if (strFirstName != "") {
    
        /* delete the cookie */ 
        document.cookie = "FirstName=" + strFirstName + "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
        document.cookie = "UserLevel=" + strUserLevel + "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
        
        //hide logout, welcome message and display login
        var aLogin = document.getElementById("aLogin");
        var aLogout = document.getElementById("aLogout");
        var divWelcome = document.getElementById("divWelcome");

        aLogin.style.display = "inline";
        aLogout.style.display = "none";
        divWelcome.style.display = "none";

        /* display logout message for 2 sec*/
        displayPopup("Thank you, " + strFirstName + "! You are now logged out", "right", 2000);
    }
}
/*****************************************************************************/